﻿@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <link href="~/Content/jquery-easyui-1.5.2/themes/default/easyui.css" rel="stylesheet" />
    <link href="~/Content/jquery-easyui-1.5.2/themes/icon.css" rel="stylesheet" />
    @*<link href="~/Content/jquery-easyui-1.5.2/demo/demo.css" rel="stylesheet" />*@
    <script src="~/Content/jquery-easyui-1.5.2/jquery.min.js"></script>
    <script src="~/Content/jquery-easyui-1.5.2/jquery.easyui.min.js"></script>
    <script src="~/Content/jquery-easyui-1.5.2/locale/easyui-lang-zh_CN.js"></script>
    <script src="~/Scripts/jquery.form.js" type="text/javascript"></script>
    <style>
        img {
            width: 180px;
            height: 220px;
        }
    </style>
</head>
<body>
    <div id="ph">
        <div style="width:100%;height:300px">
            <div style="float:left;margin-left:50px" class="">

                <form id="filePost" action="/Photos/index" method="post" enctype="multipart/form-data">
                    <p><input type="text" name="photo_Name" value="客厅" disabled="disabled" /></p>
                    <lable><input type="file" name="file" onchange="save(this);" /></lable>
                </form>
                <img name="img" alt="gfs" />
            </div>
            <div style="float:left;margin-left:50px">
                <form id="filePost" action="/Photos/index" method="post" enctype="multipart/form-data">
                    <p><input type="text" name="photo_Name" value="卧室" disabled="disabled" /></p>
                    <label> <input type="file" name="file" onchange="save(this);" /></label>
                </form>
                <img name="img" alt="" />
            </div>
            <div style="float:left;margin-left:50px">
                <form id="filePost" action="/Photos/index" method="post" enctype="multipart/form-data">
                    <p><input type="text" name="photo_Name" value="厨房" disabled="disabled" /></p>
                    <label> <input type="file" name="file" onchange="save(this);" /></label>
                </form>
                <img name="img" alt="" />
            </div>
            <div style="float:left;margin-left:50px">
                <form id="filePost" action="/Photos/index" method="post" enctype="multipart/form-data">
                    <p><input type="text" name="photo_Name" value="餐厅" disabled="disabled" /></p>
                    <label><input type="file" name="file" onchange="save(this);" /></label>
                </form>
                <img name="img" alt="" />
            </div>
        </div>
        <div style="width:100%;height:300px">
            <div style="float:left;margin-left:50px">
                <form id="filePost" action="/Photos/index" method="post" enctype="multipart/form-data">
                    <p><input type="text" name="photo_Name" value="次卧" disabled="disabled" /></p>
                    <label><input type="file" name="file" onchange="save(this);" /></label>
                </form>
                <img name="img" alt="" />
            </div>
            <div style="float:left;margin-left:50px">
                <form id="filePost" action="/Photos/index" method="post" enctype="multipart/form-data">
                    <p><input type="text" name="photo_Name" value="阳台" disabled="disabled" /></p>
                    <label> <input type="file" name="file" onchange="save(this);" /></label>
                </form>
                <img name="img" alt="" />
            </div>
            <div style="float:left;margin-left:50px">
                <form id="filePost" action="/Photos/index" method="post" enctype="multipart/form-data">
                    <p><input type="text" name="photo_Name" value="卫生间" disabled="disabled" /></p>
                    <label> <input type="file" name="file" onchange="save(this);" /></label>
                </form>
                <img name="img" alt="" />
            </div>
            <div style="float:left;margin-left:50px">
                <form id="filePost" action="/Photos/index" method="post" enctype="multipart/form-data">
                    <p><input type="text" name="photo_Name" value="玄关" disabled="disabled" /></p>
                    <label> <input type="file" name="file" onchange="save(this);" /></label>
                </form>
                <img name="img" alt="" />
            </div>
        </div>
    </div>
    <div style="width:100%;height:650px" id="imgs">

    </div>
    <div style="width:100%;">
        <hr style="border:2px solid #000000;" />
        <input id="ins" type="button" value="保存" onclick="ti(1)" veev="@ViewBag.id" style="background-color:#2596ea;width:50px;height:30px" />
        <input id="upd" type="button" value="修改" onclick="ti(2)" veev="@ViewBag.id" style="background-color:#2596ea;width:50px;height:30px" />
    </div>
</body>
</html>
<script>
    $("#upd").hide();
    var urls = "http://localhost:63805/";
    photo($("#ins").attr("veev"));
    var photo_Url = new Array();
    var photo_Name = new Array();
    function save(id) {
        var options = {
            //beforeSubmit: showRequest,
            error: showError,
            //success: showResponse,
            async: false
        };
        $(id).parent().parent().ajaxSubmit(options);
        var filepath = $(id).val();
        filepath = filepath.split("\\");
        $(id).attr("evve", "/Upload/" + filepath[2]);
        $(id).parent().parent().parent().children("[name='img']").attr("src", "/Upload/" + filepath[2])
    }
    function ti(ids) {
        var id = $("#ins").attr("veev");


        if (ids == 1) {
            photo_Name.length = 0;
            photo_Url.length = 0;
            $("[name='file']").each(function () {
                if ($(this).attr("evve") != "")
                {
                    photo_Name.push($(this).parent().prev().children().eq(0).val());
                    photo_Url.push($(this).attr("evve"));
                }
                

            })
            if (photo_Url.length < 8) {
                alert('必选项未上传');
                return;
            }
            $.ajax({
                url: "http://localhost:63805/api/Photo",
                data: JSON.stringify({ name: photo_Name, url: photo_Url, projectNumber: id }),
                contentType: "application/json",
                dataType: "json",
                type: "post",
                success: function (obj) {
                    if (obj > 0) {
                        alert("保存成功!");
                        window.history.back(-1);
                    }
                    else {
                        alert("失败!");
                    }
                }
            })
        }
        else {
            photo_Name.length = 0;
            photo_Url.length = 0;

            $("[name='file']").each(function () {
                if ($(this).attr("evve") != "") {
                    photo_Name.push($(this).parent().prev().children().eq(0).val());
                    photo_Url.push($(this).attr("evve"));
                }
            })

            if (photo_Url.length < 8) {
                alert('必选项未上传');
                return;
            }
            $.ajax({
                url: "http://localhost:63805/api/Photo/" + $("#ins").attr("veev"),
                data: JSON.stringify({ name: photo_Name, url: photo_Url, projectNumber: id }),
                contentType: "application/json",
                dataType: "json",
                type: "put",
                success: function (obj) {
                    if (obj > 0) {
                        alert("保存成功!");
                        window.history.back(-1);
                    }
                    else {
                        alert("失败!");
                    }
                }
            })
        }

    }
    $(document).ready(function () {
        var options = {

            //beforeSubmit: showRequest,
            error: showError,
            //success: showResponse
        };

        $('#filePost').submit(function () {
            $(this).ajaxSubmit(options);
            return false;
        });
    });
    //function showRequest(formData, jqForm, options) {
    //    alert('发送前');
    //    return true;
    //}
    function showError(data) {
        alert(data);
    }

    //function showResponse(responseText) {

    //    alert(responseText.sMsg);
    //}

    //反填功能
    function photo(id) {

        $.ajax({
            url: urls + "api/Photo/" + id,
            dataType: "json",
            type: "get",
            success: function (obj) {
                if (obj.length != 0) {
                    $("#ph").empty();
                    $("#upd").show();
                    $("#ins").hide();
                    for (var i = 0; i < obj.length; i++) {
                        //Array[i].attr("src", obj[i].photo_Url);
                        $("#imgs").append("<div style='float:left;margin-left:50px'><form id='filePost' action='/Photos/index' method='post' enctype='multipart/form-data'><p><input type='text' name='photo_Name' value='" + obj[i].photo_Name + "' disabled='disabled' /></p><label> <input type='file' name='file' onchange='save(this);' evve='" + obj[i].photo_Url + "'/></label></form><img name='img' alt='' src='" + obj[i].photo_Url + "' /></div>");
                    }
                } else {
                    $("#imgs").remove();
                }


            }
        })
    }///图片
</script>